package com.esionwong.basic.controller;

import com.esionwong.basic.config.GeneratorStrategy;
import com.esionwong.basic.config.properties.GeneratorProperties;
import com.esionwong.basic.framework.annotation.Module;
import com.esionwong.basic.framework.annotation.OperationLog;
import com.esionwong.basic.framework.common.api.ApiResult;
import com.esionwong.basic.framework.common.controller.BaseController;
import com.esionwong.basic.framework.common.enums.OperationLogType;
import com.esionwong.basic.framework.generate.CodeGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 代码生成控制器
 *
 * @author esionwong
 */
@Slf4j
@RestController
@RequestMapping("/gen")
@Module("system")
@Api(value = "系统管理 API", tags = {"敏捷开发"})
public class GeneratorController extends BaseController {

    /**
     * 生成代码
     *
     * @param tableName
     * @param pkIdName
     * @return
     */
    @PostMapping("/create")
    @RequiresPermissions("sys:gen:create")
    @OperationLog(name = "生成代码", type = OperationLogType.ADD)
    @ApiOperation(value = "生成代码", response = ApiResult.class)
    public ApiResult<Boolean> create (String tableName, String pkIdName) {
        GeneratorProperties generatorProperties = new GeneratorProperties();

        // 设置表信息
        generatorProperties.addTable(tableName, pkIdName);

        // 生成配置
        generatorProperties.getGeneratorConfig()
                .setGeneratorStrategy(GeneratorStrategy.SINGLE);

        // 生成代码
        CodeGenerator codeGenerator = new CodeGenerator();
        codeGenerator.generator(generatorProperties);

        return ApiResult.result(true);
    }
}
